﻿using com.ruovea.crypt;
using com.ruovea.gogs.entity;
using com.ruovea.utilty;
using com.ruovea.utilty.Common;
using com.ruovea.utilty.Exceptions;
using com.ruovea.utilty.Time;
using com.ruovea.utilty.Validate;
using PBKDF2.Key;
using System.Linq;
using System.Text;

namespace com.ruovea.gogs.service
{
    ///<summary>
    ///
    ///</summary>
    public partial class UserService : BaseServer<UserEntity>
    {
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="passwd">密码</param>
        /// <param name="email">邮箱</param>
        /// <returns></returns>
        public bool UserAdd(string name, string passwd, string email)
        {
            if (name.IsNullOrWhiteSpace())
                throw new ParamiterException("用户名不能为空.");

            if (passwd.IsNullOrWhiteSpace())
                throw new ParamiterException("密码不能为空.");

            if (passwd.Length<6)
                throw new ParamiterException("密码不能太短.");

            if (email.IsNullOrWhiteSpace())
                throw new ParamiterException("邮箱不能为空.");

            if(!Validate.email(email))
                throw new ParamiterException("邮箱格式不正确.");
            string rands = Common.RndSalt(10);
            string salt = Common.RndSalt(10);

            passwd = Security.EncryptString(passwd, salt.GetBytes(), 10000, "HMACSHA256", 50);

            return UserAdd(name, passwd, email, rands, salt);
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="passwd">密码</param>
        /// <param name="email">邮箱</param>
        /// <param name="rands">随机码</param>
        /// <param name="salt">盐</param>
        /// <returns></returns>
        private bool UserAdd(string name, string passwd, string email, string rands, string salt)
        {
            UserEntity user = new UserEntity
            {
                LowerName = name,
                Name = name,
                Email = email,
                Passwd = passwd,

                LoginSource = 0,
                LoginName = string.Empty,

                Type = 1,//待确认
                Rands = rands,

                Salt = salt,

                CreatedUnix = (int)UnixTime.ToInt64(Time.Now),
                UpdatedUnix = (int)UnixTime.ToInt64(Time.Now),

                LastRepoVisibility = 0,
                MaxRepoCreation = -1,
                IsActive = 1,
                IsAdmin = 0,
                AllowGitHook = 0,
                AllowImportLocal = 0,
                ProhibitLogin = 0,

                Avatar = Md5Crypt.Encrypt(email, false),

                AvatarEmail = email,
                UseCustomAvatar = 1,
                NumFollowers = 0,
                NumFollowing = 0,
                NumStars = 0,
                NumRepos = 0,
                Description = string.Empty,

                NumTeams = 0,//待确认
                NumMembers = 1//待确认
            };

            return this.Add(user);
        }
    }
}
